<template>
	<view class="content">
		<text class="content__title">欢迎登录！</text>
		<input class="content__input" type="text" v-model="account" placeholder="请输入您的用户名" />
		<view class="content__pwd">
			<input class="content__input" v-show="!isViewPwd" type="password" v-model="password" placeholder="请输入密码" />
			<input class="content__input" v-show="isViewPwd" type="text" v-model="password" placeholder="请输入密码" />
			<image src="../../../static/images/login/view_pwd.png" @click="viewPwd"></image>
		</view>
		<u-checkbox 
			style="margin-top: 40rpx;margin-left: 15rpx;"
			@change="checkboxChange" 
			v-model="rememberPassword" 
			active-color="green"
			name="1"
		><text style="font-size: 26rpx;
				font-weight: 500;
				color: #2D624E;
line-height: 10rpx;">记住账号密码</text></u-checkbox>
		<button class="content__btn" @click="doLogin">登录</button>
		<div class="content__bottom">
			<view class="register" @click="goRegister">注册</view>
			<view class="forget" @click="goForgotPassword">忘记密码？</view>
		</div>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				account: '',
				password: '',
				isViewPwd: false,
				rememberPassword: false,
				rememberOptions: {},
			}
		},
		mounted() {
			// #ifdef H5
			var a = document.getElementsByClassName('uni-page-head-hd')[0]
			a.style.display = 'none';
			// #endif
			
			var account = uni.getStorageSync("rememberAccount");
			var password = uni.getStorageSync("rememberPassword");
			if(account || password) {
				this.account = account;
				this.password = password;
				this.rememberPassword = true;
			}else{
				this.rememberPassword = false;
			}
			
		},
		methods: {
			checkboxChange(e) {
				this.rememberOptions = e;
			},
			goRegister() {
				uni.navigateTo({
					url: "/pages/personal/register/register"
				})
			},
			doLogin() {
				let params = {
					mobile: this.account,
					password: this.password
				}
				this.$api.post('/addons/yun_shop/api.php?route=member.login.index', params).then(res => {
					if (res.result == 1) {
						console.log(this.$Route.query);
						// window.cookieStore.get("PHPSESSID").then(res => {
						// 	commit('setToken', res.value)
						// })
						if(this.rememberOptions.value) {
							uni.setStorageSync("rememberAccount", this.account);
							uni.setStorageSync("rememberPassword", this.password);
						}else{
							uni.removeStorageSync("rememberAccount");
							uni.removeStorageSync("rememberPassword");
						}
						if (JSON.stringify(this.$Route.query) !== '{}') {
							// #ifdef APP-PLUS
							uni.switchTab({
								url: (this.$Route.query.reurl + '?id=' + this.$Route.query.id),
								fail (error) {
									console.log(error)
								}
							})
							// #endif
							// #ifdef H5
							uni.navigateTo({
								url: (this.$Route.query.reurl + '?id=' + this.$Route.query.id),
								fail (error) {
									console.log(error)
								}
							})
							// #endif
						} else {
							console.log(123123);
							// #ifdef APP-PLUS
							uni.switchTab({
								url: "/pages/prefile/prefile",
								fail (error) {
									console.log(error)
								}
							})
							// #endif
							// #ifdef H5
							uni.navigateTo({
								url: "/pages/prefile/prefile",
								fail (error) {
									console.log(error)
								}
							})
							// #endif
						}
						
					}
				})
			},
			viewPwd () {
				this.isViewPwd = !this.isViewPwd;
				console.log(this.isViewPwd);
			},
			goForgotPassword() {
				uni.navigateTo({
					url: "/pages/personal/forgot-password/forgot-password"
				})
			}
		}
	}
</script>

<style lang="scss">
	@import './login.scss'
</style>
